/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kpsmart.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import kpsmart.model.events.CostType;

/**
 * Dialog for discontinuing a route
 * 
 * @author Oliver Behncke
 */
public class DiscontinueForm extends javax.swing.JFrame {

    private final List<CostType> costTypes;
    
    /**
     * Creates new form DicontinueForm
     */
    DiscontinueForm(List<CostType> costs) {
        // store copy of costTypes in local field
        this.costTypes=new ArrayList<CostType>(costs);
        initComponents();
        // create and fill combo-box model
        DefaultComboBoxModel model=new DefaultComboBoxModel();
        int i=1;
        for(CostType c: costTypes) model.addElement(i++ +". "+ c.getFrom() + " - " + c.getTo() + " (" + c.getCompany() + ")");
        this.jComboBox1.setModel(model);
        this.jComboBox1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                // change text-field values when combobox selection has changed
                CostType c=costTypes.get(jComboBox1.getSelectedIndex());
                jTextFieldFrom.setText(c.getFrom());
                jTextFieldTo.setText(c.getTo());
                jTextFieldCompany.setText(c.getCompany());
                jTextFieldType.setText(c.getType());
            }
            
        });
        CostType c=costTypes.get(jComboBox1.getSelectedIndex());
        jTextFieldFrom.setText(c.getFrom());
        jTextFieldTo.setText(c.getTo());
        jTextFieldCompany.setText(c.getCompany());
        jTextFieldType.setText(c.getType());
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldTo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldFrom = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldType = new javax.swing.JTextField();
        jButtonSubmit = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jTextFieldCompany = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Discontinue Transport Route");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(400, 215));
        setResizable(false);

        jTextFieldTo.setEditable(false);
        jTextFieldTo.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel4.setText("From");

        jLabel5.setText("To");

        jTextFieldFrom.setEditable(false);
        jTextFieldFrom.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel13.setText("Type");

        jTextFieldType.setEditable(false);
        jTextFieldType.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jButtonSubmit.setText("Submit");

        jButtonCancel.setText("Cancel");

        jTextFieldCompany.setEditable(false);
        jTextFieldCompany.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel1.setText("Company");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Route");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jButtonSubmit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 418, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 418, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel13)
                            .addComponent(jLabel1))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldCompany, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldTo, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldType, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBox1, jTextFieldCompany, jTextFieldFrom, jTextFieldTo, jTextFieldType});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextFieldType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmit)
                    .addComponent(jButtonCancel))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jComboBox1, jTextFieldCompany, jTextFieldFrom, jTextFieldTo, jTextFieldType});

        pack();
    }// </editor-fold>//GEN-END:initComponents


    /**
     * Add ActionListener to submit button
     * @param l
     */
    void addListenerToSubmit(ActionListener l){
        this.jButtonSubmit.addActionListener(l);
    }

    /**
     * Add ActionListener to cancel button
     * @param l
     */
    void addListenerToCancel(ActionListener l){
        this.jButtonCancel.addActionListener(l);
    }

    /**
     * Get from-value of currently selected route
     * @return from as String
     */
    String getFrom(){
        return this.jTextFieldFrom.getText();
    }

    /**
     * Get to-value of currently selected route
     * @return to as String
     */
    String getTo() {
        return this.jTextFieldTo.getText();
    }

    /**
     * Get company-value of currently selected route
     * @return company as String
     */
    String getCompany() {
        return this.jTextFieldCompany.getText();
    }

    /**
     * Get type-value of currently selected route
     * @return type as String
     */
    String getRoutetype() {
        return this.jTextFieldType.getText();
    }

    /**
     * Get date-value of currently selected route
     * @return date as long (timestamp)
     */
    long getRoutedate(){
        return costTypes.get(jComboBox1.getSelectedIndex()).getDate();
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextFieldCompany;
    private javax.swing.JTextField jTextFieldFrom;
    private javax.swing.JTextField jTextFieldTo;
    private javax.swing.JTextField jTextFieldType;
    // End of variables declaration//GEN-END:variables
}
